package com.situ.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.entity.Student;
import com.situ.util.JDBCUtil;



// 这个/student就是访问这个Servlet的路径
@WebServlet("/student")
public class StudentServlet extends HttpServlet{

	// http://localhost:8080/JavaWeb/student
	// /student访问Servlet的时候默认执行service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.service()");
		// 解决post请求中文乱码问题
		req.setCharacterEncoding("UTF-8");
		
		// 首先拿到到底要访问增删改查哪个方法
		// http://localhost:8080/JavaWeb/student?method=selectAll
		// http://localhost:8080/JavaWeb/student?method=deleteById&id=3
		String method = req.getParameter("method");
		// 如果method是null或者""空字符串，让method默认是selectAll
		if (method == null || method.equals("")) {
			method = "selectAll";
		}
		
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
		default:
			break;
		}
		
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender) values(?,?,?)";
			statement = connection.prepareStatement(sql);
			// 填上？具体的值
			statement.setString(1, name);
			statement.setInt(2, Integer.parseInt(age));
			statement.setString(3, gender);
			// 返回影响的行数
			System.out.println(statement);
			int count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		// 重定向到展示所有的列表界面 /JavaWeb/student
		resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 拿到要删除的id
		// http://localhost:8080/JavaWeb/deleteStudent?id=6
		// 从浏览器拿到的数据都是字符串，即使id=6写的是数字，也是以字符串形式得到
		String id = req.getParameter("id");
		
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			statement = connection.prepareStatement(sql);
			// Integer.parseInt() 把一个字符串类型的数字解析成整数
			statement.setInt(1, Integer.parseInt(id));
			int count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		// 重定向到展示所有的列表界面 /JavaWeb/student
		resp.sendRedirect(req.getContextPath() + "/student?method=selectAll");
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			System.out.println(statement);
			list = new ArrayList<Student>();
			while (resultSet.next()) {
				// 每遍历一次就是查出数据库表里面一行数据，一列一列取出来，最后封装成一个Student对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		// 把list集合数据放到reqest这个内存区域里面
		req.setAttribute("list", list);
		// 转发到student_list.jsp进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
}
